{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.608973900Z",
     "start_time": "2023-09-10T13:45:51.018554800Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "# Author:凌逆战 | Never\n",
    "# Date: 2023/8/14\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "import sys\n",
    "sys.path.append(\"../../\")  # 添加项目根目录到 Python 路径\n",
    "import numpy as np\n",
    "from scipy import signal\n",
    "from filter.biquad import BiquadFilter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.627923Z",
     "start_time": "2023-09-10T13:45:51.617950Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 设计高通滤波器系数\n",
    "fs = 16000  # 采样率\n",
    "fc = 70  # 截止频率（Hz）\n",
    "# 输入信号\n",
    "input_signal = [0.5, 0.8, 1.0, 0.7, -0.2, -0.6, -0.8, -0.3, -0.3, -0.3, -0.3]\n",
    "\n",
    "# 定义一个二阶高通滤波器(巴特沃斯)\n",
    "b, a = signal.butter(2, fc, btype='highpass', analog=False, output='ba', fs=fs)\n",
    "\n",
    "# 创建双二阶滤波器实例\n",
    "biquad_filter = BiquadFilter(b, a)\n",
    "output_signal_biquad = [biquad_filter.process(x) for x in input_signal]\n",
    "\n",
    "# 使用signal.butter进行前向滤波\n",
    "output_signal_butter = signal.lfilter(b, a, input_signal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.655848800Z",
     "start_time": "2023-09-10T13:45:51.654851400Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Equal Outputs: True\n"
     ]
    }
   ],
   "source": [
    "# 判断两个输出结果是否相同\n",
    "print(\"Equal Outputs:\", np.allclose(output_signal_biquad, output_signal_butter, atol=1e-08))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.697736200Z",
     "start_time": "2023-09-10T13:45:51.654851400Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-10T13:45:51.706712600Z",
     "start_time": "2023-09-10T13:45:51.677788900Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
